from collections.abc import Mapping
import os
from typing import Any
from django import forms
from django.core.files.base import File
from django.db.models.base import Model
from django.forms.utils import ErrorList
from django.utils.translation import gettext_lazy as _
from appstore.models import AppStore
from projsettings.models import AppSettings

class AppStoreForm(forms.ModelForm):
    def get_sysimages(self):
        sysimg_list=[]
        image_path=AppSettings.objects.get(key='QCOW_FILE_PATH').value
        for sub_file in os.listdir(image_path):
            filelist=(sub_file,sub_file)
            sysimg_list.append(filelist)
        return tuple(sysimg_list)
    
    get_sysimages(None)
    name = forms.CharField(label=_("name"))
    type = forms.ChoiceField(label=_("type"),choices=(("os", _("OS")), ("application",_("Application"))))
    kernel = forms.CharField(label=_("kernel"))
    vcpu = forms.IntegerField(initial=2, label=_("VCPU"))
    ram = forms.IntegerField(initial=8192, label=_("Ram(MB)"))
    sysdisk = forms.IntegerField(widget=forms.HiddenInput,initial=20, label=_("SysDisk(GB)"))
    titimg = forms.ImageField(label=_('titimg'))
    volimg = forms.ChoiceField(choices=get_sysimages(None),label=_('volimg'))
   
    class Meta:
        model = AppStore
        fields = '__all__'